package net.simflow.swf.analyzer.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.layout.GridData;

public class ProcessingMessageBox extends Dialog {

	protected Object result;
	private Thread thread;
	private boolean isAlive = false;

	protected Shell shell;
	ProgressBar progressBar;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public ProcessingMessageBox(Shell parent, String title) {
		super(parent, SWT.APPLICATION_MODAL);
		setText(title);
	}

	public void setThread(Thread thread) {
		this.thread = thread;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		isAlive = true;
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.APPLICATION_MODAL);
		shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		shell.setBackgroundMode(SWT.INHERIT_FORCE);
		shell.setSize(350, 70);
		shell.setText(getText());

		shell.addListener(SWT.Traverse, new Listener() {
			@SuppressWarnings("deprecation")
			public void handleEvent(Event e) {
				if (e.detail == SWT.TRAVERSE_ESCAPE) {
					if (thread != null) {
						MessageBox dialog = new MessageBox(shell,
								SWT.ICON_WARNING | SWT.YES | SWT.NO);
						dialog.setMessage("Stop " + getText() + "?");
						if (dialog.open() == SWT.YES) {
							thread.stop();
							e.doit = true;
						} else {
							e.doit = false;
						}
					}
				}
			}
		});

		int parlocx = shell.getParent().getLocation().x;
		int parlocy = shell.getParent().getLocation().y;
		int parw = shell.getParent().getSize().x;
		int parh = shell.getParent().getSize().y;
		int thisw = shell.getSize().x;
		int thish = shell.getSize().y;
		int locx = parw / 2 + parlocx - thisw / 2;
		int locy = parh / 2 + parlocy - thish / 2;
		shell.setLocation(locx, locy);
		shell.setLayout(new FormLayout());

		Composite composite = new Composite(shell, SWT.NONE);
		FormData fd_composite = new FormData();
		fd_composite.left = new FormAttachment(0, 20);
		fd_composite.right = new FormAttachment(100, -20);
		fd_composite.top = new FormAttachment(0, 10);
		fd_composite.bottom = new FormAttachment(100, -10);
		composite.setLayoutData(fd_composite);
		composite.setLayout(new GridLayout(1, false));

		Label label = new Label(composite, SWT.NONE);
		label.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1,
				1));
		label.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label.setAlignment(SWT.CENTER);
		label.setText(this.getText() + " ... ...");

		progressBar = new ProgressBar(composite, SWT.NONE);
		progressBar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
	}

	public void close() {
		super.getParent().getDisplay().asyncExec(new Runnable() {
			public void run() {
				isAlive = false;
				shell.dispose();
			}
		});
	}

	public void setProgress(final int numerator, final int denominator) {
		super.getParent().getDisplay().asyncExec(new Runnable() {
			public void run() {
				progressBar.setMaximum(denominator);
				progressBar.setSelection(numerator);
			}
		});
	}

	public boolean isAlive() {
		return isAlive;
	}
}
